import { defineStore } from 'pinia'
import { login } from '@/http'

const id = 'user'
const initMenuActive = 'home'

const store = defineStore(id, {
  state: () => ({
    token: '',
    permissions: [],
    // 左侧菜单是否折叠
    collapse: false,
    // 菜单 name
    menuActive: initMenuActive
  }),
  getters: {},
  actions: {
    // 用户登录
    async login({ username, password }) {
      const data = await login({ username, password })
      if (data?.token) {
        this.token = data.token
        return true
      }
      return false
    },
    isLogin() {
      return !!this.token
    },
    logout() {
      this.$reset()
    }
  },
  persist: {
    key: id
    // 需要持久化存储的 key
    // paths: ['token', 'user', 'menuActive', 'tabs', 'layout', 'collapse']
  }
})

export default store
